/*
 * Copyright (c) 2025. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.action.user.query.login.flow;

import cn.hallele.infrastructure.util.NetUtil;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.infrastructure.util.bean.LoginUser;
import cn.hallele.mc.sdk.usercenter.common.enums.SexEnum;
import cn.hallele.mc.sdk.usercenter.common.enums.UserStatusEnum;
import cn.hallele.ms.portalcenter.application.bo.UserBO;
import cn.hallele.ms.starter.web.framework.node.ServiceFlow;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import lombok.extern.slf4j.Slf4j;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2025/1/2 22:22
 */
@Slf4j
@LiteflowComponent
public class QueryLoginUserInfoFlow extends ServiceFlow {
    @Override
    public void process() throws Exception {
        LoginUser loginUser = NetUtil.getLoginUser();
        UserBO result = toUserBO(loginUser);
        this.setResult(result);
    }

    private static UserBO toUserBO(LoginUser loginUser) {
        return ObjectUtil.map(loginUser, l -> UserBO.builder()
                .tenantId(l.getTenantId())
                .id(l.getId())
                .username(l.getUsername())
                .avatar(l.getAvatar())
                .phoneNumber(l.getPhoneNumber())
                .email(l.getEmail())
                .sex(ObjectUtil.map(l.getSex(), SexEnum::valueOfCode))
                .age(l.getAge())
                .status(ObjectUtil.map(l.getStatus(), UserStatusEnum::valueOfCode))
                .personalitySignature(l.getPersonalitySignature())
                .passExpiredTime(l.getPassExpiredTime())
                .accountExpiredTime(l.getAccountExpiredTime())
                .accountExpiredTime(l.getAccountExpiredTime())
                .createTime(l.getCreateTime())
                .updateTime(l.getUpdateTime())
                .createBy(l.getCreateBy())
                .updateBy(l.getUpdateBy())
                .roleCodes(l.getRoles())
                .perms(l.getPerms())
                .build());
    }
}
